package com.manager.kudian.system.service;


import com.manager.kudian.common.persistence.HibernateDao;
import com.manager.kudian.common.persistence.Page;
import com.manager.kudian.common.service.BaseService;
import com.manager.kudian.common.utils.DateUtils;
import com.manager.kudian.common.utils.PropertiesLoader;
import com.manager.kudian.common.utils.StringUtils;
import com.manager.kudian.system.dao.*;
import com.manager.kudian.system.entity.*;
import com.manager.kudian.system.vo.DeliveryItem;
import com.manager.kudian.system.vo.OrderDeliveryVO;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Administrator on 2016/4/14.
 */
@Service
@Transactional(readOnly = true)
public class OrderInfoService extends BaseService<OrderInfo, Integer> {

    private static final Logger logger = Logger.getLogger(OrderInfoService.class);

    @Autowired
    private OrderInfoDao orderInfoDao;

    @Autowired
    private OrderSellerDao orderSellerDao;

    @Autowired
    private OrderGoodsDao orderGoodsDao;

    @Autowired
    private DeliveryOrderDao deliveryOrderDao;

    @Autowired
    private DeliveryGoodsDao deliveryGoodsDao;

    @Override
    public HibernateDao<OrderInfo, Integer> getEntityDao() {
        return orderInfoDao;
    }


    public Page<OrderGoods> ordergoodsList(Page<OrderGoods> page, Integer orderId) {
        // return this.orderGoodsDao.find(Restrictions.eq("orderId", orderId));

        return orderGoodsDao.findPage(page, Restrictions.eq("orderId", orderId));
    }

    public List<OrderGoods> allordergoodsList(Integer orderId) {
        // return this.orderGoodsDao.find(Restrictions.eq("orderId", orderId));

        return orderGoodsDao.find(Restrictions.eq("orderId", orderId));
    }

    public Page<DeliveryOrder> deliveryorderlist(Page<DeliveryOrder> page, Integer orderId) {
        return deliveryOrderDao.findPage(page, Restrictions.eq("orderId", orderId));
    }

    @Transactional(readOnly = false)
    public void adddeliveryForm(OrderDeliveryVO vo) {
        DeliveryOrder d = new DeliveryOrder();
        OrderInfo oi = this.orderInfoDao.get(vo.getOrderId());
        d.setExpressName(vo.getExpressName());
        d.setDeliverySn(vo.getDeliverySn());
        d.setOrderId(vo.getOrderId());
        d.setOrderSn(oi.getOrderSn());
        d.setAddTime(DateUtils.getSysTimestamp());
        d.setManagerUserId(1);
        d.setConsignee(oi.getConsignee());
        d.setUpdateTime(DateUtils.getSysTimestamp());
        // 已发货
        d.setStatus((short)1);
        this.deliveryOrderDao.save(d);

        for (DeliveryItem di : vo.getItems()) {
            OrderGoods og = orderGoodsDao.findUnique(Restrictions.eq("orderId", vo.getOrderId()), Restrictions.eq("goodsId", di.getGoodsId()));

            DeliveryGoods dg = new DeliveryGoods();
            dg.setDeliveryId(d.getDeliveryId());
            // 已发货
            dg.setSendFlag((short) 1);
            dg.setGoodsId(di.getGoodsId());
            dg.setGoodsName(og.getGoodsName());
            dg.setGoodsSn(og.getGoodsSn());
            dg.setGoodsNumber(di.getGoodsNumber());
            dg.setGoodsPrice(og.getGoodsPrice());
            dg.setMeasureUnit(og.getMeasureUnit());

            this.deliveryGoodsDao.save(dg);
        }
    }

    @Transactional(readOnly = false)
    public void delete(Integer deliveryId) {
        DeliveryOrder d = new DeliveryOrder();
        DeliveryOrder doi = this.deliveryOrderDao.get(deliveryId);
        deliveryOrderDao.delete(doi);

        List<DeliveryGoods> dgs = deliveryGoodsDao.find(Restrictions.eq("deliveryId", deliveryId));
        for (DeliveryGoods dg : dgs) {
            this.deliveryGoodsDao.delete(dg);
        }
    }

    public Page<DeliveryGoods> deliverygoodslist(Page<DeliveryGoods> page, Integer deliveryId) {
        return deliveryGoodsDao.findPage(page, Restrictions.eq("deliveryId", deliveryId));
    }
}
